package com.fabriciojf.action;

import com.fabriciojf.controller.Action;
import com.fabriciojf.model.Category;
import com.fabriciojf.model.Status;
import com.fabriciojf.persistence.JpaUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action
 *
 * @author Fabricio S Costa fabriciojf@gmail.com
 * @since 01/12/2009
 * @version 1.0
 */
public class InserirPostagemAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {

            /*
             * passa as categorias
             */
            List<Category> categories = new ArrayList<Category>();
            String oqlcat = "from " + Category.class.getName();
            categories = JpaUtil.getEntityManager().createQuery(oqlcat).getResultList();

            /*
             * passa os status
             */
            List<Status> statuss = new ArrayList<Status>();
            String oqlstt = "from " + Status.class.getName();
            categories = JpaUtil.getEntityManager().createQuery(oqlstt).getResultList();

            request.setAttribute("categorias", categories);
            request.setAttribute("statuss", statuss);
            RequestDispatcher rd;
            rd = request.getRequestDispatcher("./adm/inserirPostagem.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            response.sendRedirect("erro.jsp");
        }

    }
}
